<template>
  <a-card :bordered="false" style="margin-left: -10px;margin-right: 0;">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="授权编码">
              <a-input placeholder="" v-model="queryParam.bussCode" allowClear></a-input>
            </a-form-item>
          </a-col>

          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <!-- [BRAND:品牌 SUPPLY:供应商 ] -->
        <span slot="bussType" slot-scope="text, record">
          <span v-if="record.bussType == 'BRAND'">品牌</span>
          <span v-if="record.bussType == 'SUPPLY'">供应商</span>
        </span>
        <span slot="bussCode" slot-scope="text, record"> [{{ record.bussCode }}]{{ record.bussName }} </span>
        <span slot="bizStatus" slot-scope="text, record">
          <a-checkbox v-model="record.bizStatus" @change="handleStatus($event, record)" />
        </span>
        <span slot="source" slot-scope="text, record">
          <a-checkbox v-model="record.sourceBool" @change="handleSource($event, record)" />
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->
  </a-card>
</template>
<script>
import '@/assets/less/TableExpand.less'
import { getAction, postAction, putAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'

export default {
  name: 'PosMerchantBussinessList',
  mixins: [JeecgListMixin],
  components: {},
  data() {
    return {
      description: '经营范围管理页面',
      disableMixinCreated: true,
      // 表头
      columns: [
        {
          title: '机构名称',
          align: 'center',
          dataIndex: 'orgName',
        },
        {
          title: '授权类型',
          align: 'center',
          dataIndex: 'bussType',
          scopedSlots: { customRender: 'bussType' },
        },
        {
          title: '授权名称',
          align: 'center',
          dataIndex: 'bussCode',
          scopedSlots: { customRender: 'bussCode' },
        },
        {
          title: '是否授权',
          align: 'center',
          dataIndex: 'bizStatus',
          scopedSlots: { customRender: 'bizStatus' },
        },
        {
          title: '切换新ERP',
          align: 'center',
          dataIndex: 'sourceBool',
          scopedSlots: { customRender: 'source' },
        },
      ],
      url: {
        list: '/sys/merchant/posMerchantBussiness/pageList',
        approve: '/sys/merchant/posMerchantBussiness/approve',
        edit: '/sys/merchant/posMerchantBussiness/edit',
      },
    }
  },
  computed: {},
  created() {},
  methods: {
    query(orgId, bussType) {
      console.log('query --> params:', orgId, bussType)
      this.$set(this.queryParam, 'orgId', orgId)
      this.$set(this.queryParam, 'bussType', bussType)
      this.loading = true
      this.loadData(1)
      setTimeout(() => {
        this.loading = false
      }, 1500)
    },
    handleStatus(e, record) {
      this.loading = true
      let toUpdate = { id: record.id }
      toUpdate.bizStatus = e.target.checked ? 1 : 0
      toUpdate.record = Object.assign(record, {
        bizStatus: toUpdate.bizStatus,
        bussType: this.queryParam.bussType,
        apprStatus: 'audit_pass',
      })

      postAction(this.url.approve, [toUpdate])
        .then((res) => {
          if (res.success) {
            this.$message.success('操作成功')
          } else {
            this.$message.warn(res.message)
          }
        })
        .finally(() => {
          this.loading = false
          this.loadData()
        })
    },
    handleSource(e, record) {
      this.loading = true
      let toUpdate = { id: record.id }
      toUpdate.source = e.target.checked ? 3 : 1
      toUpdate.record = Object.assign(record, {
        source: toUpdate.source,
        bussType: this.queryParam.bussType
      })

      putAction(this.url.edit, toUpdate)
        .then((res) => {
          if (res.success) {
            this.$message.success('操作成功')
          } else {
            this.$message.warn(res.message)
          }
        })
        .finally(() => {
          this.loading = false
          this.loadData()
        })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
